<pre style="background-color:rgb(255,255,255);font-family:'Source Code Pro';font-size:10.5pt;" data-index="0" class="" name="code"><span style="color:#000080;"><strong>from </strong></span>tkinter <span style="color:#000080;"><strong>import </strong></span>*
    <span style="color:#000080;"><strong>import </strong></span>cv2<span style="color:#808080;"><em>
    </em></span><span style="color:#000080;"><strong>from </strong></span>PIL <span style="color:#000080;"><strong>import </strong></span>Image,ImageTk
    
    
    <span style="color:#000080;"><strong>def </strong></span>take_snapshot():
        <span style="color:#000080;">print</span>(<span style="color:#008080;"><strong>"</strong></span><span style="color:#008080;font-family:'宋体';"><strong>有人给你点赞啦！</strong></span><span style="color:#008080;"><strong>"</strong></span>)
    
    <span style="color:#000080;"><strong>def </strong></span>video_loop():
        success, img = camera.read()  <span style="color:#808080;"><em># </em></span><span style="color:#808080;font-family:'宋体';"><em>从摄像头读取照片
    </em></span><span style="color:#808080;font-family:'宋体';"><em>    </em></span><span style="color:#000080;"><strong>if </strong></span>success:
            cv2.waitKey(<span style="color:#0000ff;">1000</span>)
            cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA)<span style="color:#808080;"><em>#</em></span><span style="color:#808080;font-family:'宋体';"><em>转换颜色从</em></span><span style="color:#808080;"><em>BGR</em></span><span style="color:#808080;font-family:'宋体';"><em>到</em></span><span style="color:#808080;"><em>RGBA
    </em></span><span style="color:#808080;"><em>        </em></span>current_image = Image.fromarray(cv2image)<span style="color:#808080;"><em>#</em></span><span style="color:#808080;font-family:'宋体';"><em>将图像转换成</em></span><span style="color:#808080;"><em>Image</em></span><span style="color:#808080;font-family:'宋体';"><em>对象
    </em></span><span style="color:#808080;font-family:'宋体';"><em>        </em></span>imgtk = ImageTk.PhotoImage(<span style="color:#660099;">image</span>=current_image)
            panel.imgtk = imgtk
            panel.config(<span style="color:#660099;">image</span>=imgtk)
            root.after(<span style="color:#0000ff;">1</span>, video_loop)
    
    <span style="color:#808080;font-family:'宋体';"><em>
    </em></span>camera = cv2.VideoCapture(<span style="color:#0000ff;">0</span>)    <span style="color:#808080;"><em>#</em></span><span style="color:#808080;font-family:'宋体';"><em>摄像头
    </em></span><span style="color:#808080;font-family:'宋体';"><em>
    </em></span>root = Tk()
    root.title(<span style="color:#008080;"><strong>"opencv + tkinter"</strong></span>)
    <span style="color:#808080;"><em>#root.protocol('WM_DELETE_WINDOW', detector)
    </em></span><span style="color:#808080;"><em>
    </em></span>panel = Label(root)  <span style="color:#808080;"><em># initialize image panel
    </em></span>panel.pack(<span style="color:#660099;">padx</span>=<span style="color:#0000ff;">10</span>, <span style="color:#660099;">pady</span>=<span style="color:#0000ff;">10</span>)
    root.config(<span style="color:#660099;">cursor</span>=<span style="color:#008080;"><strong>"arrow"</strong></span>)
    btn = Button(root, <span style="color:#660099;">text</span>=<span style="color:#008080;"><strong>"</strong></span><span style="color:#008080;font-family:'宋体';"><strong>点赞</strong></span><span style="color:#008080;"><strong>!"</strong></span>, <span style="color:#660099;">command</span>=take_snapshot)
    btn.pack(<span style="color:#660099;">fill</span>=<span style="color:#008080;"><strong>"both"</strong></span>, <span style="color:#660099;">expand</span>=<span style="color:#000080;"><strong>True</strong></span>, <span style="color:#660099;">padx</span>=<span style="color:#0000ff;">10</span>, <span style="color:#660099;">pady</span>=<span style="color:#0000ff;">10</span>)
    
    video_loop()
    
    root.mainloop()
    <span style="color:#808080;"><em># </em></span><span style="color:#808080;font-family:'宋体';"><em>当一切都完成后，关闭摄像头并释放所占资源
    </em></span>camera.release()
    cv2.destroyAllWindows()
    </pre>